package com.shuke.medical.insure.service;

import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shuke.medical.insure.eums.ChsInfNoEnum;
import com.shuke.medical.insure.model.dto.GetChronicDiseaseRecordDTO;
import com.shuke.medical.insure.model.dto.GetChsPersonBaseInfoDTO;
import com.shuke.medical.insure.model.dto.GetChsPersonDetaileddDTO;
import com.shuke.medical.insure.model.vo.GetChronicDiseaseRecordVO;
import com.shuke.medical.insure.model.vo.GetChsPersonBaseInfoVO;
import com.shuke.medical.insure.model.vo.GetChsPersonDetailedVO;
import com.shuke.medical.insure.remote.ChsRemoteHttp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author woo hua
 * @date 2023-03-31 15:14
 */
@Service
@Slf4j
public class ChsBaseInfoService {

    private static Object lock = new Object();

    @Resource
    private ChsRemoteHttp chsRemoteHttp;

    /**
     * 获取人员基本信息
     * @param dto
     * @return
     */
    public List<GetChsPersonBaseInfoVO> getChsPersonBaseInfo(GetChsPersonBaseInfoDTO dto) throws Exception {
        JSONObject input = new JSONObject();
        JSONObject json = writeValueAsString(dto);
        input.put("data",json);
        String res = chsRemoteHttp.doPost(ChsInfNoEnum.CHS_INF_1101, input);
        log.info("获取人员基本信息,res,[{}]",res);
        List<GetChsPersonBaseInfoVO> getChsPersonBaseInfoVOS = JSONArray.parseArray(res, GetChsPersonBaseInfoVO.class);

        return getChsPersonBaseInfoVOS;
    }

    /**
     * 获取人员详细信息
     * @param dto
     * @return
     */
    public List<GetChsPersonDetailedVO> getChsPersonDetailed(GetChsPersonDetaileddDTO dto) throws Exception {
        JSONObject input = new JSONObject();
        JSONObject json = writeValueAsString(dto);
        input.put("data",json);
        String res = chsRemoteHttp.doPost(ChsInfNoEnum.CHS_INF_1160, input);//1160
        log.info("慢特病备案查询接口,res[{}]",res);
        List<GetChsPersonDetailedVO> feedetail = toOutPutByKeyList(res, "spinfo", GetChsPersonDetailedVO.class);

        return feedetail;
    }

    /**
     * 慢特病备案查询接口
     * @param dto
     * @return
     */
    public List<GetChronicDiseaseRecordVO> getChronicDiseaseRecord(GetChronicDiseaseRecordDTO dto) throws Exception {
        JSONObject input = new JSONObject();
        JSONObject json = writeValueAsString(dto);
        input.put("data",json);
        String res = chsRemoteHttp.doPost(ChsInfNoEnum.CHS_INF_5301, input);
        log.info("慢特病备案查询接口,res[{}]",res);
        List<GetChronicDiseaseRecordVO> feedetail = toOutPutByKeyList(res, "feedetail", GetChronicDiseaseRecordVO.class);

        return feedetail;
    }



    /**
     * 入参根据指定名称序列化
     * @param ob
     * @return
     * @throws JsonProcessingException
     */
    public JSONObject writeValueAsString(Object ob) throws JsonProcessingException {
        Assert.isTrue(ob != null,
                "入参为空，请检查参数");

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.findAndRegisterModules();
        String str = objectMapper.writeValueAsString(ob);

        JSONObject jsonObject = JSONObject.parseObject(str);
        return jsonObject;
    }

    /**
     * 返回值根据指定名称和对象名称序列化成指定对象(返回值是多个对象的)
     * @param outPut
     * @param key
     * @param clz
     * @param <T>
     * @return
     * @throws JsonProcessingException
     */
    public static <T> List<T> toOutPutByKeyList(String outPut, String key, Class<T> clz){
        List<T> list = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(outPut);

        Assert.isTrue(jsonObject.containsKey(key),
                "根据key获取数据出参失败，key: [{}]不存在", key);

        String str = String.valueOf(jsonObject.get(key));
        if(StringUtils.isEmpty(str)){
            return list;
        }
        list = JSONArray.parseArray(str, clz);
        return list;
    }

}
